package cn.com.adcc.utils;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by anzilu on 2015/10/13.
 */
public class ZipHandler implements Runnable{

    private static Logger logger = Logger.getLogger(ZipHandler.class);
    private String source="";
    private String destination="";

    public ZipHandler(String source,String destination){

        this.source=source;
        this.destination=destination;
    }

    @Override
    public void run() {

        compressFiles();

    }

    public  boolean compressFiles(){

        try {
            ZipFile zipFile = new ZipFile(destination);
            ArrayList filesToAdd = new ArrayList();
            File root=new File(source);
            File[] files=root.listFiles();
            for(File f:files){
                filesToAdd.add(new File(f.getAbsolutePath()));
            }
            ZipParameters parameters = new ZipParameters();
            // set compression method to deflate compression
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
            zipFile.addFiles(filesToAdd, parameters);
            Utils.blockingQueue.add(destination);
            return true;
        }
        catch (ZipException e) {
            logger.error(e.getMessage());
            return false;
        }
    }

}
